<template>
  <div class="main-box">
    <div :class="['container', 'container-register', { 'is-txl': isLogin }]">
      <div>
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules">
        <h2 class="title">注册界面</h2>
        <el-form-item prop="userAccount">
        <input class="form__input" type="text" ref="userAccount" placeholder="用户名" v-model="registerForm.userAccount"/>
        </el-form-item>
        <el-form-item prop="password">
        <input class="form__input" type="password" ref="password" placeholder="密码" v-model="registerForm.password"/>
      </el-form-item>
      <el-form-item prop="email">
        <input class="form__input"  ref="password" placeholder="邮箱" v-model="registerForm.email"/>
      </el-form-item>
      <el-form-item prop="verifyCode">
        <input class="form__input2"  ref="verifyCode" placeholder="验证码" v-model="registerForm.verifyCode"/>
        <el-button type="primary" round @click="sendCode()" style="display: inline-block;">发送</el-button>
      </el-form-item>
      
      <el-button :loading="loading" type="primary" round @click="Register()">注册</el-button>
      </el-form>
    </div>
    </div>
    <div
      :class="['container', 'container-login', { 'is-txl is-z200': isLogin }]"
    >
    <div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
        <h2 class="title">登录界面</h2>
        <el-form-item prop="userAccount">
        <input class="form__input" type="text" ref="username" placeholder="Username" v-model="loginForm.userAccount"/>
        </el-form-item>
        <el-form-item prop="password">
        <input class="form__input" type="password" ref="password" placeholder="Password" v-model="loginForm.password"/>
      </el-form-item>
      <el-button :loading="loading" type="primary" round @click="Login()">登录</el-button>
      </el-form>
    </div>
    </div>
    <div :class="['switch', { login: isLogin }]">
      <div class="switch__circle"></div>
      <div class="switch__circle switch__circle_top"></div>
      <div class="switch__container">
        <h2>{{ isLogin ? 'Hello friend!' : 'Welcome back !' }}</h2>
        <p>
          {{
            isLogin
              ? '输入你的个人信息，加入我们！'
              : 'To keep connected with us please login with your personal info'
          }}
        </p>
        <div class="primary-btn" @click="isLogin = !isLogin">
          {{ isLogin ? '立即注册' : '立即登录' }}
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { register,sendVerifyCode } from '@/api/user'
const emailReg = /^([a-zA-Z0-9]+[-_]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
const validatePassword = (rule, value, callback) => {
  if (value.length < 6) {
    callback(new Error('密码长度不能少于6位'))
  } else {
    callback()
  }
}
const validateCode = (rule, value, callback) => {
  if (value.length===0) {
    callback(new Error('验证码不能为空'))
  } else {
    callback()
  }
}
const validateEmail = (rule, value, callback) => {
  if (emailReg.test(value)){
    callback()
  }else{
    callback(new Error('邮箱格式不正确'))
  }
}
export default {
  name: 'Login',
  data() {
return {
  loading: false,
  isLogin: true,
  loginForm: {
    userAccount: 'admin',
    password: '123456',
  },
  loginRules: {
    password: [{ required: true, trigger: 'blur', validator: validatePassword }],
    userAccount: [
        { required: true, message: '请输入用户名称', trigger: 'blur' },
        { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
      ],
  },
  registerForm: {
    userAccount: '',
    password: '',
    email:'',
    verifyCode:''
  },
  registerRules: {
    password: [{ required: true, trigger: 'blur', validator: validatePassword }],
    username: [
        { required: true, message: '请输入用户名称', trigger: 'blur' },
        { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
      ],
    email: [{ required: true, message: '请填写电子邮箱', trigger: 'change',validator: validateEmail }],
    verifyCode: [{required:true,trigger: 'blur', validator: validateCode }]
  },
  isVerifyCode:'',

}
},
methods: {
Login() {
  this.$refs.loginForm.validate(valid => {
    if (valid) {
      this.loading = true
      this.$store.dispatch('user/login', this.loginForm).then(() => {
        this.$router.push({ path: this.redirect || '/' })
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    } else {
      console.log('error submit!!')
      return false
    }
  })
},
Register() {
  this.$refs.registerForm.validate(valid => {
    if(valid){
      if(this.registerForm.verifyCode==this.isVerifyCode){
        register({data:{userAccount:this.registerForm.userAccount,password:this.registerForm.password,email:this.registerForm.email}}).then(response=>{
      this.$notify({
      title: '成功',
      message: '注册成功,自动登录',
      type: 'success'
    });
    this.$store.dispatch('user/login',this.registerForm).then(() => {
        this.$router.push({ path: this.redirect || '/' })
      })
      const { data } = response
      console.log(response)
    })
      }
      else{
        this.$message({
        message: '验证码错误！',
        type: 'error'
      });
      }
    }
    else {
      console.log('error submit!!')
      return false
    }
  })
},
sendCode() {
  if (emailReg.test(this.registerForm.email)){
    sendVerifyCode({data:{email:this.registerForm.email}}).then(response=>{
      this.isVerifyCode=response.data
      console.log(this.isVerifyCode)
      console.log(response.data)
    })
    this.$message({
        message: '发送成功，请注意查收！',
        type: 'success'
      });
  }else{
    this.$message({
        message: '警告,邮箱格式错误！',
        type: 'error'
      });
  }
}
},
}
</script>
<style lang="scss" scoped>
.main-box {
  position: absolute;
  width: 1000px;
  min-width: 1000px;
  min-height: 500px;
  height: 600px;
  padding: 25px;
  margin-left:240px;
  margin-top: 3%;
  background-color: #ecf0f3;
  box-shadow: 10px 10px 10px #d1d9e6, -10px -10px 10px #f9f9f9;
  border-radius: 12px;
  overflow: hidden;
  .container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    width: 600px;
    height: 100%;
    padding: 25px;
    background-color: #ecf0f3;
    transition: all 1.25s;
    form {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      width: 100%;
      height: 100%;
      color: #a0a5a8;
      .title {
        font-size: 34px;
        font-weight: 700;
        line-height: 3;
        color: #181818;
      }
      .text {
        margin-top: 30px;
        margin-bottom: 12px;
      }
      .form__input {
        width: 350px;
        height: 40px;
        margin: 4px 0;
        padding-left: 25px;
        font-size: 13px;
        letter-spacing: 0.15px;
        border: none;
        outline: none;
        // font-family: 'Montserrat', sans-serif;
        background-color: #ecf0f3;
        transition: 0.25s ease;
        border-radius: 8px;
        box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9;
        &::placeholder {
          color: #a0a5a8;
        }
      }
      .form__input2 {
        width: 280px;
        height: 40px;
        margin: 4px 0;
        padding-left: 25px;
        font-size: 13px;
        letter-spacing: 0.15px;
        border: none;
        outline: none;
        // font-family: 'Montserrat', sans-serif;
        background-color: #ecf0f3;
        transition: 0.25s ease;
        border-radius: 8px;
        box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9;
        &::placeholder {
          color: #a8a8a0;
        }
      }
    }
  }
  .container-register {
    z-index: 100;
    left: calc(100% - 600px);
  }
  .container-login {
    left: calc(100% - 600px);
    z-index: 0;
  }
  .is-txl {
    left: 0;
    transition: 1.25s;
    transform-origin: right;
  }
  .is-z200 {
    z-index: 200;
    transition: 1.25s;
  }
  .switch {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 400px;
    padding: 50px;
    z-index: 200;
    transition: 1.25s;
    background-color: #ecf0f3;
    overflow: hidden;
    box-shadow: 4px 4px 10px #d1d9e6, -4px -4px 10px #f9f9f9;
    color: #a0a5a8;
    .switch__circle {
      position: absolute;
      width: 500px;
      height: 500px;
      border-radius: 50%;
      background-color: #ecf0f3;
      box-shadow: inset 8px 8px 12px #d1d9e6, inset -8px -8px 12px #f9f9f9;
      bottom: -60%;
      left: -60%;
      transition: 1.25s;
    }
    .switch__circle_top {
      top: -30%;
      left: 60%;
      width: 300px;
      height: 300px;
    }
    .switch__container {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      position: absolute;
      width: 400px;
      padding: 50px 55px;
      transition: 1.25s;
      h2 {
        font-size: 34px;
        font-weight: 700;
        line-height: 3;
        color: #181818;
      }
      p {
        font-size: 14px;
        letter-spacing: 0.25px;
        text-align: center;
        line-height: 1.6;
      }
    }
  }
  .login {
    left: calc(100% - 400px);
    .switch__circle {
      left: 0;
    }
  }
  .primary-btn {
    width: 180px;
    height: 50px;
    border-radius: 25px;
    margin-top: 50px;
    text-align: center;
    line-height: 50px;
    font-size: 14px;
    letter-spacing: 2px;
    background-color: #4b70e2;
    color: #f9f9f9;
    cursor: pointer;
    box-shadow: 8px 8px 16px #d1d9e6, -8px -8px 16px #f9f9f9;
    &:hover {
      box-shadow: 4px 4px 6px 0 rgb(255 255 255 / 50%),
        -4px -4px 6px 0 rgb(116 125 136 / 50%),
        inset -4px -4px 6px 0 rgb(255 255 255 / 20%),
        inset 4px 4px 6px 0 rgb(0 0 0 / 40%);
    }
  }
}
</style>